<?php
/**
 * Created by jinqiang on 2024/8/26 10:08.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Services\Soa;


use App\Http\Models\Mysql\SsyJstPurchaseinOrderTable;
use App\Http\Models\Mysql\SsyProcessingFactoryNameTable;
use Illuminate\Support\Facades\DB;

class ReturnsService
{
    public function search($param)
    {
        $conn = (new SsyJstPurchaseinOrderTable());
        if($param['ids'] ?? []){
            $conn = $conn->whereIn('id', $param['ids']);
        }
        if(isset($param['wh_id']) && $param['wh_id'] != ""){
            $conn = $conn->where('wh_id', (Int)$param['wh_id']);
        }
        if($param['processing_code'] ?? ""){
            $conn = $conn->where('processing_code', $param['processing_code']);
        }
        if($param['warehouse'] ?? ""){
            $conn = $conn->where('warehouse', $param['warehouse']);
        }
        if($param['sku_id'] ?? ""){
            if(preg_match("/^[a-zA-Z]/is", $param['sku_id'])) {
                $conn = $conn->where('sku_id', "like", $param['sku_id']."%");
            }else{
                $conn = $conn->where('sku_id', "like", "%".$param['sku_id']."%");
            }
        }
        if($param['io_id'] ?? ""){
            $conn = $conn->where('io_id', 'like', $param['io_id']);
        }
        if($param['po_id'] ?? ""){
            $conn = $conn->where('po_id', 'like', $param['po_id']);
        }
        if($param['io_date'] ?? []){
            $start = $param['io_date'][0]." 00:00:00";
            $end = $param['io_date'][1]." 23:59:59";
            $conn = $conn->whereBetween('io_date', [$start, $end]);
        }else{
            $conn = $conn->where('io_date', '>', '2021-12-01 00:00:00');
        }
        if($param['ke_date'] ?? []){
            $start = $param['ke_date'][0]." 00:00:00";
            $end = $param['ke_date'][1]." 23:59:59";
            $conn = $conn->whereBetween('ke_date', [$start, $end]);
        }
        if($param['supplier_id'] ?? ""){
            $conn = $conn->where('supplier_id', $param['supplier_id']);
        }
        $res = $conn->orderBy('ke_date', 'desc')->paginate((Int)$param['limit'])->toArray();
        $supplierIds = array_column($res['data'], 'supplier_id');
        $factoryMap = [];
        if(!empty($supplierIds)){
            $factoryList = (new SsyProcessingFactoryNameTable())->whereIn('supplier_id', $supplierIds)->get(['supplier_id', 'factory_name'])->toArray();
            $factoryMap = array_column($factoryList, 'factory_name', 'supplier_id');
        }
        //兼容领猫供应商信息
        $factoryMap['105'] = "辉昱服装设计有限公司";
        $factoryMap['SC0019'] = "杭州布镶上下服饰有限公司2";
        $factoryMap['SW0953'] = "杭州友治实业有限公司2";
        $factoryMap['SC0020'] = "沐言智造科技（杭州）有限公司2";
        $factoryMap['SC0021'] = "杭州艺尚服装有限公司2";
        $factoryMap['SC0022'] = "杭州辰首服饰有限公司2";
        $factoryMap['SC0023'] = "杭州智衣坊服饰有限公司2";
        foreach ($res['data'] as &$item){
            $item['supplier_name'] = $factoryMap[$item['supplier_id']] ?? "";
        }
        unset($item);
        return ['data' => $res['data'], 'total' => $res['total']];
    }

    public function setDate($param)
    {
        $ids = $param['ids'];
        $type = $param['type'];
        $list = (new SsyJstPurchaseinOrderTable())->whereIn('id', $ids)->get(['id', 'io_date'])->toArray();
        DB::beginTransaction();
        foreach($list as $item){
            $save = [
                'ke_date' => $item['io_date'],
                'ke_status' => 1
            ];
            if($type == 1){//延期1个月
                $save['ke_date'] = date('Y-m-d H:i:s',strtotime($save['ke_date']."+ 1 month"));
            }elseif ($type == 2){//延期2个月
                $save['ke_date'] = date('Y-m-d H:i:s',strtotime($save['ke_date']."+ 2 month"));
            }elseif ($type == 0){//没有日期
                $save['ke_date'] = "2001-01-01 00:00:00";
            }
            if($item['io_date'] > $save['ke_date']){ //不可对账  回货日期大于对账日期
                $save['ke_status']=0;
            }
            $res = (new SsyJstPurchaseinOrderTable())->updateData((Int)$item['id'], $save);
            if($res['code'] !== 0){
                DB::rollBack();
                return $res;
            }
        }
        DB::commit();
        return ['code' => 0];
    }
}
